Function.prototype.extend = function( parentClassOrObject ){ 
    if ( parentClassOrObject.constructor == Function ) 
    { 
        //Normal Inheritance 
        this.prototype = new parentClassOrObject;
        this.prototype.constructor = this;
        this.prototype.parent = parentClassOrObject.prototype;
    } 
    else 
    { 
        //Pure Virtual Inheritance 
        this.prototype = parentClassOrObject;
        this.prototype.constructor = this;
        this.prototype.parent = parentClassOrObject;
    } 
    return this;
} 


function core(){}

core.prototype.beautyForms = true;

/*
 * Define si se usara el plugin para homogenizar los formularios y sus elementos
 */
core.prototype.setBeautyForms = function(beauty){
    this.beautyForms = beauty;
}
/*
 * Retorna TRUE/FALSE en caso de que se este usando beautyForms
 */
core.prototype.getBeautyForms = function(){
    return this.beautyForms;
}
/*
 * Return: String ID Ejem: '#mi_id'
 */
core.prototype.getId = function(textid){
    return "#"+textid;
}
/*
 * Create unique ID
 */
core.prototype.uniqueId = function(){
    var rand = Math.floor(Math.random() * 2000);
    return "Rocket-"+rand+""+(new Date()).getTime();
}

core.prototype.is_empty = function(val){
    if(val.lenght === 0 || jQuery.isEmptyObject(val) || val==="")
        return true;
    else return false;
};

core.prototype.setIdToForm = function(data,id){
    var i=data.indexOf('<form');
    if(i!=-1){//el formulario existe en la variable de texto
        var idText = '<form id="'+id+'" ';
        
        // console.log(idText);
        
        data = idText+' '+data.substring(5, data.length);
    }
    return data;
}

core.prototype.call = function(config){
    jQuery.ajax({
        url: config.url,
        type:'POST',
        success: function(data) {
            if(data!==null){
                config.success(data);
            }
        },
        error:function(data){
            if(config.error){
                config.error(data);
            }
        },
        dataFilter:function(dataType){
            if(config.dataFilter){
                config.dataFilter(dataType);
            }
            else{
                if(dataType.lenght !== 0 && !jQuery.isEmptyObject(dataType) ){
                    dataType = jQuery.parseJSON( dataType )
                }
                else{
                    console.log("sin datos validos");
                    dataType=null;
                } 
                return dataType;
            }
            
        }
    });
};

Form.prototype = new core();
Form.prototype.constructor = Form;

function Form(config){
    this.cfg = config;
    
    this.id = '';
    this.renderTo = '';
    this.autoRender = false;
    this.width = '100%';
    this.height = '100%';
    this.form = {};
    this.autoLoad = true;
    this.urlForm = '';
    this.urlAction = '';
    this.isCreated = false;
    
    this.formId = '';
    
    this.afterSuccess = null;
}
        
    if(this.cfg.renderTo) this.renderTo = this.cfg.renderTo;
    if(this.cfg.autoRender) this.autoRender = this.cfg.autoRender;
    if(this.cfg.width) this.width = this.cfg.width;
    if(this.cfg.height) this.height = this.cfg.height;
    if(this.cfg.urlForm) this.urlForm = this.cfg.urlForm;
    if(this.cfg.urlAction) this.urlAction = this.cfg.urlAction;
        
    if(this.cfg.formId) this.formId = this.cfg.formId;
        
    if(this.cfg.afterSuccess) this.afterSuccess = this.cfg.afterSuccess;
        
    this.isCreated = true;
};
this.getForm = function(config){
    var id = this.getId(config.renderTo);
    var _this = this;
    this.call({
        url:config.url,
        success:function(data){

            var form = data.data;

            if(config.beforeSuccess){
                config.beforeSuccess(data);
            }

            form = _this.setIdToForm(form, config.formId);

            $(id).html(form);

            if(_this.getBeautyForms()){
                $('#'+config.formId).jqTransform();
            }
            if(config.afterSuccess){
                config.afterSuccess(data);
            }

        //console.log("n forms: "+document.forms.length);
        }
    });
}



Form.prototype.getWidth = function(){
    return this.width;
}
Form.prototype.getHeight = function(){
    return this.height;
}
Form.prototype.getUrlForm = function(){
    return this.urlForm
    };
Form.prototype.getUrlAction = function(){
    return this.urlAction
    };
Form.prototype.setUrlForm = function(url){
    this.urlForm = url;
};
this.setUrlAction = function(url){
    this.urlAction = url;
};
Form.prototype.setWidth = function(width){
    if(width.indexOf('%')!="-1"){
        $(this.form).css('width',width);
    }
    else{
        $(this.form).css('width',width+'px');
    }
};
Form.prototype.setHeight = function(height){
    if(height.indexOf('%')!="-1"){
        $(this.form).css('width',height);
    }
    else{
        $(this.form).css('width',height+'px');
    }
};
Form.prototype._beforeLoadForm = function(data){
    return;
};
Form.prototype._getForm = function(){
    var _this = this;

    if(!this.isCreated) this._construct();
    this.getForm({
        url:this.urlForm,
        renderTo:this.renderTo,
        formId:this.formId,
        afterSuccess:function(data){
            if(_this.afterSuccess){
                _this.afterSuccess(data);
            }
        }
    });
}

Rocket.Form = {};



Rocket.Form = function(config){
    new Form(config)
}
